package com.example.demo.service;

import com.example.demo.entity.Music;
import com.example.demo.entity.MusicQuery;
import com.example.demo.entity.TypeCount;
import com.example.demo.mapper.MusicMapper;
import com.example.demo.service.MusicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MusicImpl implements MusicService {


    @Autowired
    private MusicMapper musicMapper;

    @Override
    public PageInfo<Music> searchMusics(MusicQuery query, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Music> musicList = musicMapper.selectByCondition(query);
        int total = musicMapper.countByCondition(query); // 获取总数
        return new PageInfo<>(musicList);
    }



    //首页热门歌曲展示
    @Override
    public List<Music> findMusicByDuration() {
        return musicMapper.findMusicByDuration();
    }



    //此方法用来跟新播放次数
    @Override
    public boolean incrementPlayCount(String musicId) {
        // 调用 Mapper 方法来更新数据库中的播放次数
        return musicMapper.incrementPlayCount(musicId) > 0;
    }


    @Override
    public List<Music> findMusicAll() {
        return musicMapper.findMusicAll();
    }

    @Override
    public Music findMusicID(Integer music_id) {
        return musicMapper.findMusicID(music_id);
    }



    @Override
    public List<Music> findMusicsByIDs(List<Integer> ids) {
        return musicMapper.findMusicsByIds(ids);
    }

    @Override
    public List<Music> findMusicByPopularity() {
        return musicMapper.findMusicByPopularity();
    }

    @Override
    public Integer insertMusic(Music music) {
        return musicMapper.insertMusic(music);
    }

    @Override
    public Integer updateMusic(Music music) {
        return musicMapper.updateMusic(music);
    }

    @Override
    public Integer deleteMusic(Integer music_id) {
        return musicMapper.deleteMusic(music_id);
    }
    @Override
    public List<TypeCount> getMusicTypeProportions() {
        return musicMapper.findMusicTypeCounts();
    }
}
